import pytest,logging
from tools.logger import logger
from Config.config import ApiClient
from tools.load_data import read_yaml
# 至少1个fixture用于公共前置或后置操作（如数据初始化）
@pytest.fixture(scope='function')
def client():
    return ApiClient('http://10.59.9.12:7001/')

# 任选你用过的系统，选择四个接口，使用pytest框架编写10条自动化脚本
class Test_a3mall():
    logger()
    token = None
    # 第一条用例使用参数化（@pytest.mark.parametrize）
    @pytest.mark.parametrize('data1',read_yaml(r'D:\gaosi_xm\PythonProject40\data\a3mall.yaml'))
    def test_a3mall_login_01(self,client,data1):
        logging.info('start')
        data = {"username":data1.get('username'),"password":data1.get('password')}
        login = client.post(path='/api/public/login',json_data=data)
        print(login.json())
        try:
            Test_a3mall.token = 'Bearer '+login.json()['data']['token']
        except Exception as e:
            print(e)
        assert login.status_code==200
        assert login.json()['info'] == data1.get('exp')

    def test_a3mall_login_02(self,client):
        data = {"username":'19064013675',"password":'123456'}
        login = client.post(path='/api/public/login',json_data=data)
        print(login.json())
        assert login.status_code==200
        assert login.json()['info'] == 'ok'

    def test_a3mall_login_03(self,client):
        data = {"username":'00000000',"password":'123456'}
        login = client.post(path='/api/public/login',json_data=data)
        print(login.json())
        assert login.status_code==200
        assert login.json()['info'] == '您填写的手机号码不正确！'

    # # 第二条用例使用参数化（@pytest.mark.parametrize）
    @pytest.mark.parametrize('data1',read_yaml(r'D:\gaosi_xm\PythonProject40\data\add.yaml'))
    def test_a3mall_add_04(self,client,data1):
        headers = {'auth-token':Test_a3mall.token}
        data = {"id":data1.get('id'),"sku_id":"","num":1}
        add = client.post(path='/api/cart/add',json_data=data,headers=headers)
        print(add.json())
        assert add.status_code==200
        assert add.json()['info'] == data1.get('exp')

    def test_a3mall_add_05(self,client):
        headers = {'auth-token':Test_a3mall.token}
        data = {"id":32,"sku_id":"","num":1}
        add = client.post(path='/api/cart/add',json_data=data,headers=headers)
        print(add.json())
        assert add.status_code==200
        assert add.json()['info'] == '商品添加至购物车成功'

    def test_a3mall_add_06(self,client):
        headers = {'auth-token':''}
        data = {"id":32,"sku_id":"","num":1}
        add = client.post(path='/api/cart/add',json_data=data,headers=headers)
        print(add.json())
        assert add.status_code==200
        assert add.json()['info'] == '您还未登录，请登录'

    def test_a3mall_updata_07(self,client):
        headers = {'auth-token':Test_a3mall.token}
        data = {"id":32,"sku_id":0,"num":"2"}
        updata = client.post(path='/api/cart/change',json_data=data,headers=headers)
        print(updata.json())
        assert updata.status_code==200
        assert updata.json()['info'] == 'ok'

    def test_a3mall_updata_08(self,client):
        headers = {'auth-token':Test_a3mall.token}
        data = {"id":32,"sku_id":0,"num":"2",'spid':'01'}
        updata = client.post(path='/api/cart/change',json_data=data,headers=headers)
        print(updata.json())
        assert updata.status_code==200

    def test_a3mall_delete_09(self,client):
        headers = {'auth-token': Test_a3mall.token}
        data = {"id":"200"}
        delete = client.post(path='/api/cart/delete',json_data=data,headers=headers)
        print(delete.json())
        assert delete.status_code==200
        assert delete.json()['info'] == 'ok'

    def test_a3mall_delete_10(self,client):
        headers = {'auth-token': Test_a3mall.token}
        data = {"id":"###"}
        delete = client.post(path='/api/cart/delete',json_data=data,headers=headers)
        print(delete.json())
        assert delete.status_code==200
        logging.info('last')

if __name__ == '__main__':
    pytest.main()
